<?php

namespace app\manage\controller;

use core\libs\db;
use core\libs\input;

class ads_position extends base {
    // 初始化
    public function __init() {
        $this->input = input::request(
            'id', 'action', 
            'search_key',
            'position_name', 'position_describe', 'position_time'
        );

        // 是否有权限
        $this->is_level($this->baseClass . "_list");
        if(in_array($this->baseAction, ["submit", "delete", "batch"])) {
            $this->is_level($this->baseClass . "_edit");
        }
    }
    // 列表
    public function index() {
        extract($this->input);

        $filter = '';	
        if($search_key) {
            $filter.=" and position_name like '%$search_key%'";
        }

        $sql = "1 = 1 $filter";
        $page = new \core\libs\page((int)db::getCount("ads_position", $sql), 10);
        $page->args = $this->getArgs("", 1);
        $result = db::fetchAll(
            "ads_position", 
            "$sql order by position_id" . $page->limit
        );

        $this->assign("search", [
            'key' => $search_key
        ]);
        $this->assign("global", [
            "page" => $page,
        ]);
        $this->assign("result", $result);
        $this->display("ads_position_list");
    }
    // 编辑
    public function edit() {
        extract($this->input);

        $this->assign("row", db::fetch("ads_position", "position_id = '$id'"));
        $this->assign("global", [
            "id" => $id
        ]);
        $this->display("ads_position_add");
    }
    // 添加、更新
    public function submit() {
        extract($this->input);

        $form_datas = array(
            'position_name' => $position_name, 
            'position_describe' => $position_describe,
            'position_time' => (int)strtotime($position_time)
        );
        foreach($form_datas as $key => $val) {
            $form_datas[$key] = html_entity_decode($val);
        }

        if(empty($id)){
			//添加内容
			db::insert("ads_position", $form_datas);	
			alert('内容添加成功!', U($this->baseClass, $this->getArgs()));		
		}
		else{
			//更新	
			db::update("ads_position", $form_datas, "position_id = '$id'");
			alert('内容更新成功!', U($this->baseClass, $this->getArgs()));
		}
    }
    // 删除
    public function delete() {
        extract($this->input);

        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        //删除内容
		db::delete('ads_position', "position_id in ($id)");

        alert('删除成功!', U($this->baseClass, $this->getArgs()));		
    }
    // 批量操作
    public function batch() {
        extract($this->input);

        if(!$action) alert('请选择要操作的事件!', U($this->baseClass, $this->getArgs()));
        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        // 删除
        if($action == "delete") {
            $this->delete();
        }
    }
    // 获取固定地址栏参数
    protected function getArgs($args = "", $type = 0) {
        extract($this->input);
        $baseurl = "?search_key={$search_key}{$args}";
        return $this->get_base_args($baseurl, $type);
    }
}